let express=require("express");
let serves=require("express")();
let ejs = require("ejs");
let mysql = require('mysql');

//获取放置在public文件夹下的静态文件,
serves.use(express.static(__dirname + '/public'));

// 规划链接
let sql = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'company',
  port:3307,
  timezone:"08:00"
});
 
// 尝试链接
sql.connect();

serves.get("/caselist",(request,response)=>{
	sql.query("SELECT *from exam",(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("public/exam.html",{caseList:data}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			//console.log(result)
			response.end(result)
		});
	})
})

serves.get("/casedetails",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT *from exam where id=${id}`,(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("public/examdetails.html",{infolist:data[0]}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			//console.log(result)
			response.end(result)
		});
	})
})

serves.get("/solulist",(request,response)=>{
	sql.query("SELECT *from solution",(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("public/solu.html",{soluList:data}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			console.log(result)
			response.end(result)
		});
	})
})

serves.get("/soludetails",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT *from solution where id=${id}`,(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("public/soludetails.html",{infosoluList:data[0]}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			console.log(result)
			response.end(result)
		});
	})
})
//真正将上面的计划执行监听到本机的81端口上
serves.listen(8080);